package com.yufei.utils;
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */



import java.net.URI;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class URIResolver{
    protected final Log log = LogFactory.getLog(this.getClass());
    static final String[] IGNORED_PREFIX = new String[]{"javascript:", "mailto:"};
    protected URI base;
    protected LinkNormalizer linkNormalizer = new LinkNormalizer();

    public URIResolver(URI base) {
        if (base == null) throw new IllegalArgumentException("base cannot be null");
        this.base = base;
    }

    public URI resolve(String link) {
        URI result = null;
        try {
            String target = linkNormalizer.normalize(link);
            target = linkNormalizer.escape(target);

            for (String ignored : IGNORED_PREFIX) {
                if (target.indexOf(ignored) != -1) return null;
            }

            int queryStringIndex = target.indexOf('?'); // to detect if http:// exists in query string
            int namedAnchorIndex = target.length() > 0 ? target.indexOf('#') : -1;
            if (namedAnchorIndex != -1 && namedAnchorIndex > queryStringIndex) target = target.substring(0, namedAnchorIndex);

            if (target.length() == 0) {
                result = base;
            } else if (target.substring(0, queryStringIndex != -1 ? queryStringIndex : target.length()).indexOf("://") != -1) {
                result = new URI(target);
            } else {
                if (target.charAt(0) == '?') {
                    target = base.getPath() + target;
                } else if (target.startsWith("./")) {
                    target = target.substring("./".length(), target.length());
                }

                if ((base.getPath() == null || "".equals(base.getPath())) && !target.startsWith("/")) {
                    target = "/" + target;
                }


                result = base.resolve(target);
            }
        } catch (Exception e) {
            log.error("URI is not valid - link: " + link + ", base: " + base + ", e: " + e.getMessage());
        }
        if (log.isTraceEnabled())
            log.trace("resolve() - resolved - result: " + result + ", link: " + link + ", base: " + base);
        return result;
    }

    public String toString() {
        return super.toString() + ": base: " + base;
    }

}                  
